# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

#
# class XiaohuaPipeline:
#     def process_item(self, item, spider):
#         return item
from scrapy.pipelines.images import ImagesPipeline
import scrapy
import os


class ImgsPipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        print(item)
        yield scrapy.Request(item['src'], meta={'item': item})

    def file_path(self, request, response=None, info=None, *, item=None):
            item = request.meta['item']
            file_name = item['src'].split('/')[-1]
            file_name = os.path.join(item['title'].replace(" ", ""), file_name)
            # name = '/'.join(file_name)
            print(file_name)
            return file_name

    def item_completed(self, results, item, info):
            return item
